# encoding: utf-8
'''
Created on 2018年6月5日

@author: mengqiang.song
'''
import os
import Config
import time
import random
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' #屏蔽掉tensorFlow的一些警告

import numpy as np
from PIL import Image 

#1.获取文件夹列表
#2.获取所有的文件夹的两两组合
#3.将图片路径做一个标记组成数组。

class ImgInfo(object):
    def __init__(self,imgPath,parentPath):
        self.imgPath = imgPath
        self.parentPath = parentPath
    def compare(self,imgInfo):
        """
                   文件相同、返回true
                   文件不同返回false
        """
        if self.parentPath == imgInfo.parentPath :
            return True
        return False
    
class ImgTrainInfo(object):
    
    def __init__(self,ImgInfo1,ImgInfo2):
        self.m1 = ImgInfo1
        self.m2 = ImgInfo2
        if ImgInfo1.parentPath == ImgInfo2.parentPath :
            self.label=[1,0]#true 相同
        else:
            self.label=[0,1]#false 不同
        
    def getm1Array(self):
        L = Image.open(self.m1.imgPath)
        imgdata=np.matrix(L.getdata(),dtype='float')
        return imgdata
    def getm2Array(self):
        L = Image.open(self.m2.imgPath)
        imgdata=np.matrix(L.getdata(),dtype='float')
        return imgdata
    def getLabel(self):
        return self.label
    
class ImgPath(object):
    @staticmethod 
    def buildImgInfos(path):
        imgInfoArray = []
        for imgParentPath in os.listdir(path):
            imgParentPath = path+"\\"+imgParentPath
            for imgPath in os.listdir(imgParentPath):
                imgInfo = ImgInfo(imgParentPath+"\\"+imgPath,imgParentPath)
                imgInfoArray.append(imgInfo)
        return imgInfoArray
    
    @staticmethod
    def buildImgTrainInfos(imgInfoArray):
        print("traindata->class impPath->method buildImgTrainInfos 接收到的图片数目：",str(len(imgInfoArray)))
        trainInfos = []
#         k = 0
        imgInfoArray[1]
        for i in imgInfoArray:
            imgInfo1 = i
            for j in imgInfoArray:
                imgInfo2 = j
                t = ImgTrainInfo(imgInfo1,imgInfo2)
                trainInfos.append(t)
#                 print(str(k))
#                 k = k+1
        print("traindata->class impPath->method buildImgTrainInfos 生成的测试样例数目：",str(len(trainInfos)))
        return trainInfos
    
class TrainData(object):    
    def __init__(self,path,batchSize):
        imgInfoArray = ImgPath.buildImgInfos(path)
        imgInfoArray2 = ImgPath.buildImgTrainInfos(imgInfoArray)
        print('打乱训练数据')
        random.shuffle(imgInfoArray2)
        self.imgInfoArray=imgInfoArray2
        self.batchSize=batchSize
    
    def __iter__(self):
        pself=self
        class MyIter(object):
            def __init__(self):
                self.arrayIndex = 0
            def __next__(self):
                if self.arrayIndex >= len(pself.imgInfoArray):
                    raise StopIteration()
                batchX1 = np.zeros([pself.batchSize,72*72])
                batchX2 = np.zeros([pself.batchSize,72*72])
                batchY = np.zeros([pself.batchSize,2])
                print("迭代索引值：",self.arrayIndex," 待处理样例数目：",len(pself.imgInfoArray))
                _arrayIndex = 0
                for i in range(pself.batchSize):
                    _arrayIndex = i+self.arrayIndex
                    if _arrayIndex >= len(pself.imgInfoArray):
                        self.arrayIndex = _arrayIndex+1
                        return batchX1,batchX2,batchY
                    imgInfo = pself.imgInfoArray[_arrayIndex]
                    img_gray1 = imgInfo.getm1Array()  / 255
                    img_gray2 = imgInfo.getm2Array() / 255
                    r1 =  np.array(np.reshape(img_gray1 , [-1 , 72*72])).reshape(72*72)
                    r2 =  np.array(np.reshape(img_gray2 , [-1 , 72*72])).reshape(72*72)
                    batchX1[i,:]=r1
                    batchX2[i,:]=r2
                    batchY[i,:]=imgInfo.getLabel()  
                self.arrayIndex = _arrayIndex+1
                return batchX1,batchX2,batchY
            
        return MyIter()



L = Image.open("E:\\git\\code12306\\src\\app\\temp\\train_data\\pic0.jpg")
# print(L.size)
# imgdata=np.matrix(L.getdata(),dtype='float')
# np.asarray(a, dtype, order)
imgdata= np.array(L.getdata(),dtype='int')
print(imgdata.shape)
imgdata = np.reshape(imgdata, [72,72])
print(imgdata.shape)
for li in imgdata:
    line = ""
    for e in li:
        line+=str(e*6/255)+",\t"
    print(line)
# k = ""
# for i in imgdata:
#     k=k+str(i)+","
# print(k)
    
    